<template>
  <el-tabs v-model="activeName" v-loading="loading">
    <el-tab-pane label="图片配置" name="files">
      <el-card class="box-card">
        <div slot="header"  class="clearfix">
          <span class="uni-card-title">商户图片</span>
          <el-button style="float: right; padding: 3px 0" type="text" @click="addFile('merchantFileConfig')">新增</el-button>
        </div>
        <el-table :data="merchantFileConfig" border size="medium" class="table">
          <el-table-column prop="label" show-overflow-tooltip label="名称">
          </el-table-column>
          <el-table-column prop="value" show-overflow-tooltip label="键值">
          </el-table-column>
          <el-table-column prop="fileType" show-overflow-tooltip label="文件类型 ">
            <template slot-scope="scope">
              {{ $dictUtils.getDictLabel("file_type", scope.row.fileType, '-') }}
            </template>
          </el-table-column>
          <el-table-column prop="format" show-overflow-tooltip label="资源格式">
          </el-table-column>
          <el-table-column prop="limit" show-overflow-tooltip label="数量限制">
          </el-table-column>
          <el-table-column header-align="center" align="center" fixed="right" width="200" label="操作">
            <template slot-scope="scope">
              <el-button type="text" icon="el-icon-edit" size="small" @click="editFile(scope.row, scope.$index,'merchantFileConfig')">修改</el-button>
              <el-button type="text" icon="el-icon-delete" size="small" @click="delFile(scope.$index,'merchantFileConfig')">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-button style="float: right; margin:20px 10px;" type="primary" @click="submitConfig('merchantFileConfig', merchantFileConfig)">保存</el-button>
      </el-card>
      <el-card class="box-card">
        <div slot="header"  class="clearfix">
          <span class="uni-card-title">公共设施</span>
          <el-button style="float: right; padding: 3px 0" type="text" @click="addFile('utilitieFileConfig')">新增</el-button>
        </div>
        <el-table :data="utilitieFileConfig" border size="medium" class="table">
          <el-table-column prop="label" show-overflow-tooltip label="名称">
          </el-table-column>
          <el-table-column prop="value" show-overflow-tooltip label="键值">
          </el-table-column>
          <el-table-column prop="fileType" show-overflow-tooltip label="文件类型 ">
            <template slot-scope="scope">
              {{ $dictUtils.getDictLabel("file_type", scope.row.fileType, '-') }}
            </template>
          </el-table-column>
          <el-table-column prop="format" show-overflow-tooltip label="资源格式">
          </el-table-column>
          <el-table-column prop="limit" show-overflow-tooltip label="数量限制">
          </el-table-column>
          <el-table-column header-align="center" align="center" fixed="right" width="200" label="操作">
            <template slot-scope="scope">
              <el-button type="text" icon="el-icon-edit" size="small" @click="editFile(scope.row, scope.$index,'utilitieFileConfig')">修改</el-button>
              <el-button type="text" icon="el-icon-delete" size="small" @click="delFile(scope.$index,'utilitieFileConfig')">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-button style="float: right; margin:20px 10px;" type="primary" @click="submitConfig('utilitieFileConfig', utilitieFileConfig)">保存</el-button>
      </el-card>
      <el-card class="box-card">
        <div slot="header"  class="clearfix">
          <span class="uni-card-title">业态图片</span>
          <el-button style="float: right; padding: 3px 0" type="text" @click="addFile('merchantTypeFileConfig')">新增</el-button>
        </div>
        <el-table :data="merchantTypeFileConfig" border size="medium" class="table">
          <el-table-column prop="label" show-overflow-tooltip label="名称">
          </el-table-column>
          <el-table-column prop="value" show-overflow-tooltip label="键值">
          </el-table-column>
          <el-table-column prop="fileType" show-overflow-tooltip label="文件类型 ">
            <template slot-scope="scope">
              {{ $dictUtils.getDictLabel("file_type", scope.row.fileType, '-') }}
            </template>
          </el-table-column>
          <el-table-column prop="format" show-overflow-tooltip label="资源格式">
          </el-table-column>
          <el-table-column prop="limit" show-overflow-tooltip label="数量限制">
          </el-table-column>
          <el-table-column header-align="center" align="center" fixed="right" width="200" label="操作">
            <template slot-scope="scope">
              <el-button type="text" icon="el-icon-edit" size="small" @click="editFile(scope.row, scope.$index,'merchantTypeFileConfig')">修改</el-button>
              <el-button type="text" icon="el-icon-delete" size="small" @click="delFile(scope.$index,'merchantTypeFileConfig')">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-button style="float: right; margin:20px 10px;" type="primary" @click="submitConfig('merchantTypeFileConfig', merchantTypeFileConfig)">保存</el-button>
      </el-card>
    </el-tab-pane>
    <el-tab-pane label="语言配置" name="language">
      <el-card class="box-card">
        <el-form ref="languageConfig" label-width="140px">
          <el-row :gutter="15">
            <el-form-item label="语言名称">
              <el-col :span="14">
                <el-input value="中文" readonly="" disabled=""></el-input>
              </el-col>
              <el-col :span="10">
                <el-button type="success" icon="el-icon-plus" circle v-if="languageConfig.length === 0"
                  @click="addItem('languageConfig')" style="margin: 3px 0;"></el-button>
              </el-col>
            </el-form-item>
            <el-form-item label="语言名称" v-for="(language,index) in languageConfig" :key="index">
              <el-col :span="7">
                <el-input v-model="language.label" placeholder="语言名称"></el-input>
              </el-col>
              <el-col :span="7">
                <el-input v-model="language.value" placeholder="语言键值"></el-input>
              </el-col>
              <el-col :span="10">
                <el-button type="danger" icon="el-icon-minus" circle @click="deleteItem(index, 'languageConfig')"
                  style="margin: 3px;"></el-button>
                <el-button type="success" icon="el-icon-plus" circle
                  v-if="index === languageConfig.length-1 && languageConfig.length < 3" @click="addItem('languageConfig')"
                  style="margin: 3px;"></el-button>
              </el-col>
            </el-form-item>
          </el-row>
        </el-form>
        <el-button style="float: right; margin:0px 160px 20px 0px;" type="primary" @click="submitConfig('languageConfig', languageConfig)">保存</el-button>
      </el-card>
    </el-tab-pane>
    <el-tab-pane label="授权管理" name="secret">
      <el-card class="box-card">
        <el-form ref="secretKey"  :model="secretKey" label-width="140px">
          <el-row :gutter="15">
            <el-col :span="16">
              <el-form-item label="导视屏授权KEY" prop="kioskForbid"
                :rules="[
                       {required: true, message:'导视屏授权KEY不能为空', trigger:'blur'}
                ]">
                <el-input v-model="secretKey.kioskForbid" placeholder="导视屏授权KEY"     ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6" style="line-height: 40px;">
              有效期：{{secretConfig.kioskForbid}}
            </el-col>

            <el-col :span="16">
              <el-form-item label="管理后台授权KEY" prop="manageForbid"
                :rules="[
                       {required: true, message:'管理后台授权KEY不能为空', trigger:'blur'}
              ]">
                <el-input v-model="secretKey.manageForbid" placeholder="管理后台授权KEY"     ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6" style="line-height: 40px;">
              有效期：{{secretConfig.manageForbid}}
            </el-col>
          </el-row>
        </el-form>
        <el-button style="float: right; margin:0px 80px 20px 10px;" type="primary" @click="submitConfig('secretKey', secretKey)">保存</el-button>
      </el-card>
    </el-tab-pane>
    <el-tab-pane label="后台配置" name="manage">
      <el-card class="box-card">
        <el-form ref="manageConfig"  :model="manageConfig" label-width="140px">
          <el-row :gutter="15">
            <el-col :span="22">
              <el-form-item label="后台功能" prop="functionList"
                :rules="[
                 ]">
                <el-checkbox-group
                    v-model="manageConfig.functionList">
                  <el-checkbox label="导购大屏" key="导视大屏">导购大屏</el-checkbox>
                  <el-checkbox label="小程序" key="小程序">小程序</el-checkbox>
                  <el-checkbox label="人员定位" key="人员定位">人员定位</el-checkbox>
                </el-checkbox-group>
              </el-form-item>
            </el-col>
            <el-col :span="22">
              <el-form-item  label="Lora在线" prop="onlineTime">
                 <el-input-number v-model="manageConfig.onlineTime" :step="1"  controls-position="right" :min="2" label="Lora在线"></el-input-number>
                 <div class="uni-notice">超过这个时间算工牌离线（单位：秒）</div>
              </el-form-item>
            </el-col>
            <el-col :span="22" v-for="userType in loraUserTypeList" :key="userType.id">
              <el-form-item :label="userType.label" :prop="userType.value"
                   :rules="[
                    ]">
                 <el-upload-ext
                   :limit = "1"
                   accept = ".png"
                   fileType = "2"
                   width = "256"
                   height = "256"
                   v-model = "manageConfig['userType'+userType.value]"
                 >
                 </el-upload-ext>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-button style="float: right; margin:0px 80px 20px 10px;" type="primary" @click="submitConfig('manageConfig', manageConfig)">保存</el-button>
      </el-card>
    </el-tab-pane>
    <!-- 弹窗, 新增 / 修改 -->
    <JfFileConfigForm  ref="jfFileConfigForm" @onComplete="onComplete"></JfFileConfigForm>
  </el-tabs>
</template>
<script>
  import JfFileConfigForm from './JfFileConfigForm'
  import ElUploadExt from '@/components/elUploadExt'
  export default {
    data() {
      return {
        loading: false,
        activeName: 'files',
        languageConfig: [],
        merchantFileConfig: [],
        utilitieFileConfig: [],
        merchantTypeFileConfig: [],
        secretKey: {
          "kioskForbid": '',
          "manageForbid": ''
        },
        secretConfig: {
          "kioskForbid": '',
          "manageForbid": ''
        },
        manageConfig: {
          functionList: [],
          userType1: '',
          userType2: '',
          userType3: '',
          userType4: '',
          angle: 180,
          centerX: 0,
          centerY: 0,
          onlineTime: 1200,
          angleCorrect: 0
        },
        loraUserTypeList:[]
      }
    },
    components: {
      JfFileConfigForm,
      ElUploadExt
    },
    activated() {
      this.refreshList()
    },
    mounted() {
      this.loraUserTypeList = this.$dictUtils.getDictList("lora_user_type")
    },
    methods: {
      // 获取数据列表
      refreshList() {
        this.loading = true
        this.$http({
          url: '/smis/config/jfConfig/list',
          method: 'get',
          params: {
            'pageNo': 1,
            'pageSize': 1000,
          }
        }).then(({
          data
        }) => {
          if (data && data.success) {
            data.page.list.forEach((config) => {
              if(this.$data.hasOwnProperty(config["keyStr"])) {
                this.$data[config["keyStr"]] = JSON.parse(config["valueStr"])
              }
            })
          }
          this.loading = false
        })
      },
      submitConfig(keyStr, value) {
        this.loading = true
        this.$http({
          url: `/smis/config/jfConfig/saveConfig`,
          method: 'post',
          data: {
            keyStr: keyStr,
            valueStr: JSON.stringify(value)
          }
        }).then(({
          data
        }) => {
          if (data && data.success) {
            this.$message.success(data.msg)
            this.refreshList()
          }
          this.loading = false
        })
      },
      onComplete(method, row, index, groupType) {
        if(method === 'add') {
          this.$data[groupType].push(row);
        } else {
          this.$data[groupType].splice(index, 1, row);
        }
      },
      /************************** 语言配置 **************************/
      addItem(groupType) {
        var item = {
          "label": "",
          "value": ""
        }
        this.$data[groupType].push(item)
      },
      deleteItem(index, groupType) {
        this.$data[groupType].splice(index, 1);
      },
      /************************** 文件配置 **************************/
      addFile(groupType) {
        this.$refs.jfFileConfigForm.init('add', '', 0,groupType)
      },
      editFile(row, index, groupType) {
        this.$refs.jfFileConfigForm.init('edit', row, index, groupType)
      },
      delFile(index, groupType) {
        this.$data[groupType].splice(index, 1);
      }
    }
  }
</script>
<style type="text/css">
  .box-card {
    margin-bottom: 10px;
  }
  .uni-card-title {
    font-weight: bold;
    font-size: 16px;
  }
  .uni-notice {
    color: #999999;
    font-size: 12px;
    line-height: 20px;
  }
</style>
